package patrol.mobile.com.mobilepatrol.activity.security.emergency;


import android.content.Intent;
import android.view.View;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.activity.CommonListActivity;
import patrol.mobile.com.mobilepatrol.activity.security.ConsTitles;
import patrol.mobile.com.mobilepatrol.activity.security.adapter.EmergencyPackageEdit1Adapter;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.bean.HistoryBean;
import patrol.mobile.com.mobilepatrol.utils.GsonUtil;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

public class EmergencyPackageEdit1Activity extends CommonListActivity {
    private String holderId = "";
    private String perName = "";

    @Override
    protected BaseQuickAdapter initAdapter() {
        return new EmergencyPackageEdit1Adapter(this);
    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    protected String initTitleName() {
        return ConsTitles.EMERGENCY_HISTORY;
    }

    @Override
    public void initTitle() {
        super.initTitle();
    }

    @Override
    public void setListener() {
        super.setListener();
        Intent intent = getIntent();
        if (intent != null) {
            holderId = intent.getStringExtra("holderId");
            perName = intent.getStringExtra("perName");
            requestData(holderId);
        }

    }

    @Override
    public void configAdapter() {
        super.configAdapter();
    }

    private void requestData(String holderId) {
        HttpSender sender = new HttpSender(this, baseUrl, Urls.IEMERGENCY_HISTORY);
        HashMap<String, String> map = new HashMap<>();
        map.put("holderId", holderId);
        sender.requestPost(map, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                JsonParser jp = new JsonParser();
                JsonObject jo = jp.parse(response).getAsJsonObject();
                JsonArray listArray = jo.getAsJsonArray("list");
                List<HistoryBean> history2Beans = new ArrayList<>();
                List<HistoryBean> historyBeans = GsonUtil.jsonToList(listArray.toString(), HistoryBean.class);
                for (int i = 0; i < historyBeans.size(); i++) {
                    HistoryBean historyBean = historyBeans.get(i);
                    if (historyBean.getHouseholder().equals(perName)) {
                        history2Beans.add(historyBean);
                    }
                }
                mAdapter.updateItems(history2Beans);
                mAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
                    @Override
                    public void onItemClick(View view, int position) {
                        HistoryBean historyBean = (HistoryBean) mAdapter.getData().get(position);
                        Intent intent=new Intent(EmergencyPackageEdit1Activity.this,EmergencyPackageEdit4Activity.class);
                        intent.putExtra("id",historyBean.getId());
                        startActivity(intent);

                    }
                });
            }
        });
    }


}
